package com.example.test;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.Manifest;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.test.adapter.IndexMainAdapter;
import com.example.test.broadcast.MyReceiver;
import com.example.test.fragment.MusicFragment;
import com.example.test.fragment.OneFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ViewPager vp;
    private RadioGroup rg;
    private RadioButton rb1;
    private RadioButton rb2;
    private RadioButton rb3;
    private RadioButton rb4;

    //fragment集合
    private List<Fragment> fragmentList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        vp = (ViewPager) findViewById(R.id.vp);
        rg = (RadioGroup) findViewById(R.id.rg);
        rb1 = (RadioButton) findViewById(R.id.rb1);
        rb2 = (RadioButton) findViewById(R.id.rb2);
        rb3 = (RadioButton) findViewById(R.id.rb3);
        rb4 = (RadioButton) findViewById(R.id.rb4);

        //动态方式注册广播接收者
        //暂停
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.notify.pause");
        MyReceiver myReceiver = new MyReceiver();
        registerReceiver(myReceiver,intentFilter);
        //继续
        IntentFilter intentFilter1 = new IntentFilter();
        intentFilter1.addAction("com.notify.restart");
        MyReceiver myReceiver1 = new MyReceiver();
        registerReceiver(myReceiver1,intentFilter1);
        //上一首
        IntentFilter intentFilter2 = new IntentFilter();
        intentFilter2.addAction("com.notify.back");
        MyReceiver myReceiver2 = new MyReceiver();
        registerReceiver(myReceiver2,intentFilter2);
        //下一首
        IntentFilter intentFilter3 = new IntentFilter();
        intentFilter3.addAction("com.notify.next");
        MyReceiver myReceiver3 = new MyReceiver();
        registerReceiver(myReceiver3,intentFilter3);

        //初始化fragmentList集合
        fragmentList = new ArrayList<>();

        //动态获取权限
        requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},100);

        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            //切换界面改变按钮状态
            @Override
            public void onPageSelected(int position) {
                if (position == 0){
                    rb1.setChecked(true);
                }else if (position == 1){
                    rb2.setChecked(true);
                }else if (position == 2){
                    rb3.setChecked(true);
                }else if (position == 3){
                    rb4.setChecked(true);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        //点击按钮切换界面
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb1:
                        vp.setCurrentItem(0);
                        break;
                    case R.id.rb2:
                        vp.setCurrentItem(1);
                        break;
                    case R.id.rb3:
                        vp.setCurrentItem(2);
                        break;
                    case R.id.rb4:
                        vp.setCurrentItem(3);
                        break;
                }
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 100 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            //添加fragment
            fragmentList.add(new MusicFragment());
            for (int i = 0; i < 3; i++) {
                fragmentList.add(new OneFragment());
            }
            //添加适配器
            IndexMainAdapter indexMainAdapter = new IndexMainAdapter(getSupportFragmentManager(), fragmentList);
            vp.setAdapter(indexMainAdapter);
        }else {
            finish();
        }
    }
}